package com.hotec.PROJECT_NAME.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hotec.PROJECT_NAME.component.exception.exc.BizException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 黑山老妖
 * @date 2018/9/25
 */
public class UtilsWeb {
    /**
     * content转换为json，并使用response返回内容
     *
     * @param response
     * @param content
     */
    public static void responseJson(HttpServletResponse response, Object content) {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter out=null;
        try {
            out = response.getWriter();
            out.println(new ObjectMapper().writeValueAsString(content));
        } catch (IOException e) {
            UtilsLog.log(UtilsWeb.class, e);
            throw new BizException("io错误");
        }finally {
            if(out != null){
                out.flush();
                out.close();
            }
        }
    }
}
